<?
layout("/inc/layout.html",{title:sysTitle}){
?>

<div id="page-wrapper">
	<!--BEGIN TITLE & BREADCRUMB PAGE-->
	<div id="title-breadcrumb-option-demo" class="page-title-breadcrumb">
		<div class="page-header pull-left">
			<div class="page-title">用户组管理</div>
		</div>
		<ol class="breadcrumb page-breadcrumb pull-left">
			<li><i class="fa fa-home"></i>&nbsp;<a href="main.html">首页</a>&nbsp;&nbsp;<i
				class="fa fa-angle-right"></i>&nbsp;&nbsp;</li>
			<li>系统管理&nbsp;&nbsp;<i class="fa fa-angle-right"></i>&nbsp;&nbsp;</li>
			<li class="active">用户组管理</li>
		</ol>

		<div class="clearfix"></div>
	</div>
	<!--END TITLE & BREADCRUMB PAGE-->
	<!--BEGIN CONTENT-->
	<div class="page-content">


		<div class="row" >
			<div class="col-lg-4">
	            <div class="panel" style="height: 540px; overflow: auto;">
	                <div class="panel-heading">用户组列表
	                	<p style="float: right;">
		                	<button  type="button" onclick="addUserGroup()" class="btn btn-green btn-square" id="savejiedian">新建</button>&nbsp;
		                	<button  type="button" onclick="uploadeRolegRoup()" class="btn btn-green btn-square" id="savejiedian">编辑</button>&nbsp;
		                	<button  type="button"   class="btn btn-green btn-square"  onclick="deleteUserGroup()" >删除</button>
	                	</p>
						
	                </div>
	                <div class="panel-body" >
	                	<ul id="tree" class="ztree" style="width:270px"; overflow:auto;"></ul>
	                </div>
	            </div>
	        </div>
     
			 <div class="col-lg-4">
				<div class="panel" style="height: 540px;overflow: auto;">
				
					<div class="panel-heading">角色列表
					<input  type="button"  style="float: right" ; value="保存" class="btn btn-green btn-square"  onclick="uploadrole();" />
					</div>
					<div > 
					<table class="table">
                       <tbody id="roles">
					  </tbody>
                     </table>
                     </div>
				</div>
			</div>
	        
	        <div class="col-lg-4">
	            <div class="panel" style="height: 540px; overflow: auto;">
	                <div class="panel-heading">用户列表</div>
	                <div class="panel-body" >
	                	<div id="menuButtion" style="padding-left: 4%">
	                		<span class='subtitle' style="color: #CCC;" >数据为空！请选择用户列表</span>
	                	</div>
	                </div>
	            </div>
	        </div>
		</div>

	<!--END CONTENT-->
</div>
</div>

<?} ?>
<script>
	var id;
	var ztreeId;
	var parentname =1;
	function getlay() {
		return id;
	}
	var ztreename;
	function getztreename() {
		return ztreename;
	}
	function getztreeId() {
		return ztreeId;
	}
	$(function() {
		initTree();
		queryRoles();
	});
	var userId;
	function getUserId() {
		return userId;
	}
	function initTree() {
		$.ajax({
			type : "post",
			data : {},
			url : "knowledgeEditDataRoleQueryUserGroup.do",
			dataType : "json",
			beforeSend : function(XMLHttpRequest) {
			},
			success : drawTree,
		});
	}

	function drawTree(data) {
	//alert(JSON.stringify(data));
		var setting = {
			view : {
				dblClickExpand : false,
			},
			data : {
				simpleData : {
					enable : true,
				}
			},
			check : {
				enable : false
			},
			callback : {
				onClick : onClick
			},
		};
		var zNodes = eval(data);
		$.fn.zTree.init($("#tree"), setting, zNodes).expandAll(true);
		zTree = $.fn.zTree.getZTreeObj("tree");
		zTree.expandNode(zTree.getNodes()[0], true);

	}
	
	function onClick(e, treeId, treeNode) {
		ztreeId = treeNode.id;	
		ztreename = treeNode.name;	
		queryRoles(ztreeId);
		queryAllUser(treeNode.id)
		 if(treeNode.children==null){
       		parentname=0;
       	 }else{
         	parentname=1;
        }
	}
	//编辑
	function uploadeRolegRoup() {
		if(ztreeId ==null){
			layer.msg('请选择要编辑用户！');
		}else{
			layer.open({
			type : 2,
			area : [ '400px', '400px' ],
			fix : false, //不固定
			scrollbar : false,
			shadeClose : true,
			move : false,
			title:"编辑用户组",
			skin : 'layui-layer-rim', //加上边框
			btn : [ '保存 ', '关闭' ],
			content : ["knowledge-edit-data-uploadroleGroup.html", 'no'],
			yes : function(index,layer) {		
			 var iframeWin = parent.window[layer.find('iframe')[0]['name']];
			  			  iframeWin.page();
			  			  ztreename="根目录";
						  ztreeId=0;
			},
			cancel : function(index) {
			}
		});
		}
	}

	
	//删除
	function deleteUserGroup(){
		if(ztreeId ==null){
			layer.msg('请选择要删除用户！');
		}else{
			if (parentname == 0) {
				if (confirm("是否确认删除！！")){
					$.ajax({
						type : "post",
						data : {
						"id":ztreeId
						},
						url : "knowledgeEditDataRoleDeleteUserGroup.do",
						dataType : "json",
						beforeSend : function(XMLHttpRequest) {},
						success : function(data){
							if(data ==0){
								layer.msg('删除成功！');
							}else{
								layer.msg('有关联！无法删除');
							}
						             initTree();
			           		 },
					});
				
					//location.href="knowledgeEditDataRoleDeleteUserGroup.do?id="+ztreeId; 
				}
			} else {
				alert("有子分类不允许删除");
			}
		}
	}
	
	//加用户组
	function addUserGroup() {
		layer.open({
			type : 2,
			area : [ '400px', '400px' ],
		    scrollbar: false,
		    shadeClose: true,
			fix : false, //不固定
			title : '添加用户组',
			//maxmin : true,  
			skin: 'layui-layer-rim', //加上边框
    	    btn: ['保存 ', '关闭'],
			content : ["knowledge-edit-data-adduser.html",'no'],
			yes: function(index,layer){
	                var iframeWin = parent.window[layer.find('iframe')[0]['name']];
	                 iframeWin.save();
	                 ztreename="根目录";
					 ztreeId=0;
	         },
			cancel : function(index) {
			}
			
		});
	}
	//新增用户
	function addUser(lid) {
		id = lid;
		layer.open({
			type : 2,
			area : [ '400px', '400px' ],
		    scrollbar: false,
		    shadeClose: true,
			fix : false, //不固定
			title : '用户组管理',
			//maxmin : true,  
			skin: 'layui-layer-rim', //加上边框
    	    btn: ['保存 ', '关闭'],
			content : ["knowledge-edit-data-addrole.html",'no'],
			yes: function(index,layer){
	                var iframeWin = parent.window[layer.find('iframe')[0]['name']];
	                 iframeWin.save();
	         },
			cancel : function(index) {
			}
			
		});
	}
	
	//查询角色
	function queryRoles(id){
			$.ajax({
			type : "post",
			data : {
			"id":id
			},
			url : "knowledgeEditDataQueryUserGroupRole.do",
			dataType : "json",
			beforeSend : function(XMLHttpRequest) {
			},
			success : function(data){
			//alert(JSON.stringify(data));
			var str = "";   
      		 $("#roles").html("");
       			for(var i=0;i<data.length;i++){			
           		 str+="<tr><td>";
           		 	if(data[i].pitch == 1){
           		 		str+="<input type='checkbox' name='checkall' checked='true' value='"+data[i].roleId+"'/>&nbsp;&nbsp;&nbsp;";
           		 	}else{
           		 		str+="<input type='checkbox' name='checkall' value='"+data[i].roleId+"'/>&nbsp;&nbsp;&nbsp;";
           		 	}
                    str+= "<a>"+data[i].roleName+"</a>"   
                 /*    onclick='queryUser("+data[i].roleId+")'  */             
                    +"</td></tr>"                   
           		 }
           	$("#roles").append(str);
			}
		});
	}
	
	//编辑角色列表
	function uploadrole(){
		var value=[];
		$("input[name='checkall']:checked").each(function(){
			value.push($(this).val());
		});
		if(ztreeId ==null){
			layer.msg('请选择用户组！');
		}else{
			$.ajax({
				type : "post",
				data : {
					"roleId" : value,
					"id" : ztreeId
				},
				url : "knowledgeEditDataEditUserGroupRole.do",
				dataType : "json",
				traditional : true,
				beforeSend : function(XMLHttpRequest) {},
				success : function(data){
					initTree();
					layer.msg('保存成功');
				},
			});
			
		}	
	}
	
	//角色查询用户
	function queryUser(rid){
		$.ajax({
			type : "post",
			url : "knowledgeEditDataQueryUser.do",
			dataType : "json",
			data :{
				"id" : rid
			},
			beforeSend : function(XMLHttpRequest) {
			},
			success : function(data){
				//alert(JSON.stringify(data));
				var str = "";
				$("#menuButtion").html("");
				for ( var i = 0; i < data.length; i++) {
					str += "<input class='btn btn-blue btn-square' onclick='querybutton("+data[i].id+")' type='button' value="+data[i].name+" />&nbsp;&nbsp;&nbsp;";
				}
				$("#menuButtion").append(str);
			},
		});
	}
	//查询全部用户
	function queryAllUser(usid){
		$.ajax({
			type : "post",
			url : "knowledgeEditDataQueryAllUser.do",
			dataType : "json",
			data :{
				"id" : usid
			},
			beforeSend : function(XMLHttpRequest) {
			},
			success : function(data){
				//alert(JSON.stringify(data));
				var str = "";
				$("#menuButtion").html("");
				for ( var i = 0; i < data.length; i++) {
					str += "<input style='margin-top: 10px;margin-left:6px;' class='btn btn-blue btn-square' onclick='queryUserDetails("+data[i].id+")' type='button' value="+data[i].name+" />";
				}
				$("#menuButtion").append(str);
			},
		});
	}
	function queryUserDetails(queryUserDetailsId){
		userId = queryUserDetailsId;
		layer.open({
			type : 2,
			area : [ '820px', '600px'  ],
			fix : false, //不固定
			scrollbar : false,
			shadeClose : true,
			move : false,
			title:"用户详情",
			skin : 'layui-layer-rim', //加上边框
			btn : ['关闭'],
			content : ["knowledge-edit-data-queryuserdetails.html", 'no'],
			yes : function(index,layer) {		
			 var iframeWin = parent.window[layer.find('iframe')[0]['name']];
			  			  iframeWin.save();
			},
			cancel : function(index) {
			}
		});
		
	}
</script>